using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

namespace CodePlex.Resourceful.Amazon.S3
{

    public class Bucket
    {

        private readonly string _name;
        private readonly DateTime _creationDate;

        public  Bucket(string name, DateTime creationDate) {
            _name = name;
            _creationDate = creationDate;
        }

        public string Name
        {
            get
            {
                return _name;
            }
        }

        public DateTime CreationDate
        {
            get
            {
                return _creationDate;
            }
        }
  

        public static Bucket Parse(XmlReader reader)
        {
            string currentBucketName = null;
            DateTime currentBucketDate = DateTime.MinValue;

            while (reader.Read())
            {
                if (reader.NodeType == XmlNodeType.Element)
                {
                    if (reader.Name == "Name")
                    {
                        currentBucketName = Utils.ReadString(reader);
                    }
                    else if (reader.Name == "CreationDate")
                    {
                        currentBucketDate = DateTime.Parse(Utils.ReadString(reader));
                    }
                }

                if (reader.NodeType == XmlNodeType.EndElement && reader.Name == "Bucket")
                {
                    return new Bucket(currentBucketName, currentBucketDate);
                }

            }

            throw CodePlexException.Format("Failed to parse bucket.");

        }
    }
}
